<?php
namespace app\member\controller;
use think\Controller;
use app\index\controller\Base;

Class Order extends Base
{


   public function orderList(){
      $order=db('tp_order');
   	$uid=session('uid');

      //条件筛选
      $map=array();
      $status=input('status');
      if(!$status){
         $status=1;
      }
      $map['order_del']=0;
      switch ($status)
      {
         case 1://全部订单
         $map['user_id']=$uid;
         break;  
         case 2://待付款
         $map['user_id']=$uid;
         $map['pay_status']=0;
         break;
         case 3://待收货
         $map['user_id']=$uid;
         $map['post_status']=1;
         break;
         case 4: //已完成
         $map['user_id']=$uid;
         $map['post_status']=2;
         break;
      }

   	$orderInfo=db('tp_order')->field('id,out_trade_no,order_tptal_price,pay_status,post_status,consignee,order_time')->where($map)->paginate(3);
   	$render=$orderInfo->render();//分页
   	$orderInfo=$orderInfo->items();

   	foreach ($orderInfo as $k => $v) {
   		$orderInfo[$k]['goods']=db('tp_order_goods')->alias('og')->field('og.*,g.mid_thumb')->join('tp_goods g','og.goods_id=g.id')->where(['order_id'=>$v['id']])->select();
   	}
      $orderNum=array();
      //全部订单
      $totalOrder=$order->field('count(id) totalOrder')->where(['user_id'=>$uid,'order_del'=>0])->find();
      //待付款
      $notPayOrder=$order->field('count(id) notPayOrder')->where(['user_id'=>$uid,'order_del'=>0,'pay_status'=>0])->find();
      //待收货
      $notGetOrder=$order->field('count(id) notGetOrder')->where(['user_id'=>$uid,'order_del'=>0,'post_status'=>1])->find();
      //已完成
      $GetOrder=$order->field('count(id) GetOrder')->where(['user_id'=>$uid,'order_del'=>0,'post_status'=>2])->find();

      $orderNum['totalOrder']=$totalOrder['totalOrder'];
      $orderNum['notPayOrder']=$notPayOrder['notPayOrder'];
      $orderNum['notGetOrder']=$notGetOrder['notGetOrder'];
      $orderNum['GetOrder']=$GetOrder['GetOrder'];
   	
   	$this->assign([
   		'orderInfo'=>$orderInfo,
   		'render'=>$render,
         'orderNum'=>$orderNum
   	]);


    return view();
   }


   public function orderInfo($id){
   	$uid=session('uid');
   
   	$orderInfo=db('tp_order')->find($id);
   	$orderInfo['goods']=db('tp_order_goods')->alias('og')->field('og.*,g.mid_thumb')->join('tp_goods g','og.goods_id=g.id')->where(['order_id'=>$orderInfo['id']])->select();

   	// dump($orderInfo);die;
   	$this->assign([
   		'orderInfo'=>$orderInfo
   	]);


   	return view();
   }



   public function orderDel($id){
      db('tp_order')->where('id',$id)->update(['order_del'=>1]);
      $this->success('删除订单成功',url('member/Order/orderList'));
      
   }


   public function address(){
      $uid=session('uid');
      $address=db('tp_address')->where('user_id',$uid)->find();
      $this->assign('address',$address);
      if(request()->isPost()){
         $data=input('post.');
         unset($data['button']);

         $up=db('tp_address')->where('user_id',$uid)->update($data);

            $this->success('更新地址成功',url('member/Order/address'));
      

         
      }
      return view();
   }













}